package com.gidgabs.modules.api.service.impl;

import com.gidgabs.common.utils.Assist;
import com.gidgabs.common.utils.DeleteType;
import com.gidgabs.common.utils.LangUtil;
import com.gidgabs.common.utils.ReturnJson;
import com.gidgabs.dto.TbProductDto;
import com.gidgabs.modules.api.service.TbProductService;
import com.gidgabs.modules.api.service.TbUserService;
import com.gidgabs.modules.dao.TbProductDao;
import com.gidgabs.modules.entity.TbProductEntity;
import com.gidgabs.modules.entity.TbUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class TbProductServiceImpl implements TbProductService {
	@Autowired
	private TbProductDao productDao;

	@Autowired
	private TbUserService userService;
	
	@Override
	public TbProductEntity queryObject(Integer productId){
		return productDao.queryObject(productId);
	}

	@Override
	public TbProductEntity queryObject(TbProductEntity productEntity) {
		return productDao.queryByObject(productEntity);
	}

	@Override
	public List<TbProductEntity> queryList(Map<String, Object> map){
		return productDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return productDao.queryTotal(map);
	}
	
	@Override
	public void save(TbProductEntity product){
		productDao.save(product);
	}
	
	@Override
	public void update(TbProductEntity product){
		productDao.update(product);
	}

	@Override
	public void updateBatch(Integer[] productIds) {
		productDao.updateBatch(productIds);
	}

	@Override
	public void delete(Integer productId){
		productDao.delete(productId);
	}
	
	@Override
	public void deleteBatch(Integer[] productIds){
		productDao.deleteBatch(productIds);
	}

	@Override
	public List<TbProductEntity> selectTbProductEntity(TbProductEntity product) {
		return productDao.selectTbProductByObj(product);
	}

	@Override
	public TbProductEntity selectTbProductById(Integer id) {
		return productDao.selectTbProductById(id);
	}

	@Override
	public ReturnJson<TbProductDto> queryAdvertiseList(Integer pageSize, Integer pageNo, Integer fromTypeId) {
		Assist assist = new Assist();
		//根据广告费排序
		Assist.WhereOrder advertisePrice = Assist.order("advertise_price", false);
		assist.andEq("delete_type", DeleteType.NOT_DELETE.getKey())
				.andEq("source_user_id", fromTypeId);
		assist.setOrder(advertisePrice);
		assist.setRowSize(pageSize);
		assist.setStartRow(LangUtil.firstLimit(pageSize, pageNo));
		List<TbProductEntity> tbProductEntities = productDao.selectTbProduct(assist);
		long count = productDao.getTbProductRowCount(assist);
		List<TbProductDto> tbProductDtos = new ArrayList<>();
		for (TbProductEntity asd : tbProductEntities) {
			TbProductDto tbUserAdvertiseDto = asd.toDto();
			//获取广告主人和头像地址
			TbUser tbUser = userService.selectTbUserById(tbUserAdvertiseDto.getUserId());
			tbUserAdvertiseDto.setMasterName(tbUser.getUserName());
			tbUserAdvertiseDto.setHeadImgUrl(tbUser.getHeadImgUrl());
			tbProductDtos.add(tbUserAdvertiseDto);
		}
		return ReturnJson.success("查询成功", tbProductDtos, count, pageSize, pageNo);
	}

	@Override
	public int selectForwardNumber(String productCode) {
		return productDao.selectForwardNumber(productCode);
	}

	@Override
	public ReturnJson<TbProductDto> queryMyProductList(Integer pageSize, Integer pageNo, TbUser tbUser) {
		Assist assist = new Assist();
		//根据创建时间排序
		Assist.WhereOrder createTime = Assist.order("create_time", false);
		assist.andEq("delete_type", DeleteType.NOT_DELETE.getKey())
				.andEq("user_id",tbUser.getUserId());
		assist.setOrder(createTime);
		assist.setRowSize(pageSize);
		assist.setStartRow(LangUtil.firstLimit(pageSize, pageNo));
		List<TbProductEntity> tbProductEntities = productDao.selectTbProduct(assist);
		long count = productDao.getTbProductRowCount(assist);
		List<TbProductDto> tbProductDtoList = new ArrayList<>();
		for (TbProductEntity asd : tbProductEntities) {
			tbProductDtoList.add(asd.toDto());
		}
		return ReturnJson.success("查询成功", tbProductDtoList, count, pageSize, pageNo);
	}

}
